package com.pj.testcomposeapplication

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.fontResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import com.pj.testcomposeapplication.ui.theme.TestComposeApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestComposeApplicationTheme {
                ContentCompose(){
                    showToast(it)
                }
            }
        }
    }
    fun showToast(text:String){
        Toast.makeText(this,text,Toast.LENGTH_LONG).show()
    }
}


@Composable
fun Greeting(name: String,action1:(s:String)->Unit={}) {
   Row(
       Modifier
           .padding(10.dp)
           .clickable { action1(name) }) {
       Image(painter = rememberImagePainter(data = "https://pic3.zhimg.com/v2-76e178e62912ee589d691d81cb7587cd_r.jpg"), contentDescription = "头像",Modifier.size(60.dp))
       Column(
           Modifier
               .weight(1f)
               .height(IntrinsicSize.Max).align(Alignment.CenterVertically)
               .padding(horizontal = 10.dp)) {
           Text(text = "Hello $name!",fontSize = 16.sp,modifier = Modifier
               .width(IntrinsicSize.Max)
               .height(IntrinsicSize.Max)
               .padding(bottom = 15.dp)
//               .weight(1f)
               .background(Color.White),color = Color.Black)
           Text(text = "Hello $name!",fontSize = 16.sp,modifier = Modifier
               .width(IntrinsicSize.Max)
               .background(Color.White),color = Color.Gray)
       }
   }
}

@Composable
fun ContentCompose(action1:(s:String)->Unit={}){
    Column() {
        TopAppBar(title = {Text("测试主页",color = Color.White,fontSize = 18.sp)},navigationIcon = {
            IconButton(onClick = { action1("返回") }) {
                Icon(Icons.Filled.ArrowBack,"返回")
            }
        },actions = {
            IconButton(onClick = {action1("设置")}) {
                Icon(Icons.Filled.Settings, contentDescription = "设置")
            }
        })
        Box(Modifier.weight(1f)){
            LazyColumn{
                items(listOf("1","2","3","4","5")){
                    Greeting(name = it,action1=action1)
                }
            }
        }
        BottomAppBar {
            BottomNavigation {
                BottomNavigationItem(selected = true, onClick = {  },icon= {Icon(
                    painter = painterResource(id = R.mipmap.message),
                    contentDescription = "消息"
                )},label = {Text("消息")})
                BottomNavigationItem(selected = false, onClick = {  },icon= {Icon(
                    painter = painterResource(id = R.mipmap.contacts),
                    contentDescription = "联系人"
                )},label = {Text("联系人")})
                BottomNavigationItem(selected = false, onClick = {  },icon= {Icon(
                    painter = painterResource(id = R.mipmap.person),
                    contentDescription = "我"
                )},label = {Text("我")})
            }
        }
    }

}

sealed class Screen(val route: String, val title: String,) {
    object Message : Screen("message","消息")
    object Contacts : Screen("contacts", "通讯录")
    object Person : Screen("person", "我")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
   ContentCompose()
}